﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ChoWorkflowServer.Model;
using Cinchoo.Core.Text;
using MahApps.Metro.Controls;

namespace ChoWorkflowDashboard
{
    /// <summary>
    /// Interaction logic for ChoTaskDependencyWindow.xaml
    /// </summary>
    public partial class ChoTaskDependencyWindow : MetroWindow
    {
        private readonly string _taskName;
        public readonly ChoTasksDataSet.CHO_TASK_SCHEDULESRow Row;
        private bool _canClose = true;

        public ChoTaskDependencyWindow(string taskName, ChoTasksDataSet.CHO_TASK_SCHEDULESRow row, string title = null)
        {
            InitializeComponent();

            lblAvailFuncs.Text = GetHelpText();
            _taskName = taskName;
            if (!title.IsNullOrWhiteSpace())
                this.Title = title;

            Row = row;
            this.DataContext = Row;
        }

        private string GetHelpText()
        {
            StringBuilder msg = new StringBuilder();

            msg.AppendLine("Available operators:");
            msg.AppendLine("&&, ||, !, =, !=, <, <=, >, >=".Indent());
            msg.AppendLine();

            msg.AppendLine("Available functions:");
            msg.AppendLine("EXITCODE(job), SUCCESS(job), FAILED(job), ABORTED(job), STOPPPED(job), TIMEDOUT(job), NOTRUNNING(job)".Indent());

            return msg.ToString();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            _canClose = true;

            if (!txtInput.Text.IsNullOrWhiteSpace())
            {
                //Check for circular dependency
                if (txtInput.Text.Contains("({0})".FormatString(_taskName)))
                {
                    _canClose = false;
                    lblErrMsg.Text = "Can't contain reference itself.";
                    return;
                }

                using (ChoLexParser lex = new ChoLexParser(txtInput.Text))
                {
                    try
                    {
                        bool x = lex.Evaluate();
                    }
                    catch (Exception ex)
                    {
                        _canClose = false;
                        lblErrMsg.Text = ex.Message;
                        return;
                    }
                }
            }

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _canClose = true;
            ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable table = Row.Table as ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable;

            if (Row.RowState == System.Data.DataRowState.Added)
            {
                Row[table.DEPENDENCIESColumn] = DBNull.Value;
                Row[table.FIRE_AFTER_DEPENDENCYColumn] = 0;
            }
            else if (Row.RowState == System.Data.DataRowState.Modified)
            {
                Row[table.DEPENDENCIESColumn] = Row[table.DEPENDENCIESColumn, System.Data.DataRowVersion.Original];
                Row[table.FIRE_AFTER_DEPENDENCYColumn] = Row[table.FIRE_AFTER_DEPENDENCYColumn, System.Data.DataRowVersion.Original];
            }
            this.Close();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !_canClose;
        }
    }
}
